import { message } from 'antd';

import {
  selectResource,
  selectResourceAction,
  insertResourceAction,
  updateResourceAction,
  removeResourceAction,
  removeResource,
} from '../services';

export default {
  namespace: 'resourceActionManageModel',

  state: {
    resourceList: [],
    resourceTotal: 0,
  },

  effects: {
    *selectResource({ payload }, { put, call }) {
      const res = yield call(selectResource, payload);
      const { code, data, message: errMsg } = res;
      if (code === 0) {
        const { total, dataList } = data;
        yield put({
          type: 'select',
          payload: {
            resourceList: dataList,
            resourceTotal: total,
          },
        });
      } else {
        message.error(errMsg);
      }
    },

    *selectResourceAction({ payload }, { call }) {
      return yield call(selectResourceAction, payload);
    },

    *insertResourceAction({ payload }, { call }) {
      return yield call(insertResourceAction, payload);
    },

    *updateResourceAction({ payload }, { call }) {
      return yield call(updateResourceAction, payload);
    },

    *removeResourceAction({ payload }, { call }) {
      return yield call(removeResourceAction, payload);
    },

    *removeResource({ payload }, { call }) {
      return yield call(removeResource, payload);
    },
  },
  reducers: {
    /* 更新状态（通用） */
    select: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
};
